package com.splic.api.controller.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.splic.generator.controller.GenController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Loubing
 * Date 2021/7/12 18:50
 */
@Service
@Slf4j
public class LoginTokenService {

    @Value("${jwt.secret.key:EUdsM7lOJMLfZ411UxwwX83kUQYYaEXO}")
    private String secretKey;

    public String getSecretKey() {
        return secretKey;
    }

    /**
     * 生成token
     * @param userId
     * @return
     */
    public static String grAuthorization(Long userId, String account,HttpServletRequest request) {
        /*HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        String tokenData = UserContext.createJWT(userId.toString(), mobile, businessNo,864000L);
        Cookie cookie = new Cookie(UserContext.JWT_AUTHORIZATION, tokenData);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(10 * 3600 * 24);//过期时间为7天

        String domain = request.getServerName();//直接用确定的域名
        String cookieDomain = domain;
        // 获取当前请求的下一级域名，最低 二级域名
//        if (StringUtils.countMatches(domain, ".") > 1) {
//            cookieDomain = StringUtils.substringAfter(domain, ".");
//        }
        cookie.setDomain(cookieDomain);
        response.addCookie(cookie);
        return tokenData;*/
        String token="";
        token= JWT.create().withAudience(userId+"")
                .sign(Algorithm.HMAC256(account));
        return token;
    }

    /**
     * 注销登录
     */
    public void resetCookie(){
        /*HttpServletRequest request = RequestContext.getHttpServletRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Cookie[] allCookie = request.getCookies();
        if (allCookie != null && allCookie.length > 0) {
            for (int i = 0; i < allCookie.length; i++) {
                Cookie cookie = allCookie[i];
                if (cookie != null && cookie.getName().equals(UserContext.JWT_AUTHORIZATION)) {
                    log.info("clear cookie Authorization:{}", cookie.getValue());
                    cookie = new Cookie(UserContext.JWT_AUTHORIZATION, "1");
                    cookie.setPath("/");
                    cookie.setHttpOnly(true);
                    // 过期时间为0秒
                    cookie.setMaxAge(0);
                    String domain = request.getServerName();
                    String cookieDomain = domain;
                    cookie.setDomain(cookieDomain);
                    response.addCookie(cookie);
                }
            }
        }*/
    }

}
